package com.endurance.factory;

import com.endurance.enums.PayEnums;
import com.endurance.service.PaymentService;
import com.endurance.service.impl.AliPayService;
import com.endurance.service.impl.WxPayService;

/**
 * @Author: sunlin
 * @Date: 2024-05-06 8:45
 * @Version: 1.0
 * @Description:
 */
public class PaymentFactory {


    public static PaymentService getPaymentService(String payType) {
        // 此处可以使用策略模式进行重构
        PaymentService paymentService = null;
        switch (payType) {
            case "ALI":
                paymentService = new AliPayService();
                break;
            case "WX":
                paymentService = new WxPayService();
                break;
        }
        return paymentService;
    }

    /**
     * 使用策略模式优化以上代码
     */
    public static PaymentService getPayStrategy(String strategyType) {
        String className = PayEnums.valueOf(strategyType).getPayMode();
        try {
            // 获取具体的策略
            return (PaymentService) Class.forName(className).newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
